<?php
/**
 * Get all relations from current object and parse relations.tpl.
 *
 * PHP versions 5
 *
 * Copyright (C)
 * This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation. Either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. Without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. Ff not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * Copyright (C) 2006-2007
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2008-04-17
 * @package railbrowser
 */

function ajax_call( $ajaxBridge ) {
	global $object, $smarty;

	// return if object isn't readable
	if( !$object->is_readable() ){
		$ajaxBridge->append( 'html_code', '<p>Sie haben nicht die erforderlichen Rechte.</p>' );
		return;
	}

	// return if object has no relations
	if( $object->count_relations() <= 0 ){
		$ajaxBridge->append( 'html_code', '<p>Das Objekt hat keine Beziehungen.</p>' );
		return;
	}

	// relations
	$relations = array();
	foreach( $object->get_relation_types() as $relation_type ){

		// all relations
		$objs = array();
		foreach( $object->get_relations( $relation_type ) as $relation )
			$objs[] = array(
				'id' => $relation->get_id(),
				'path' => $relation->get_path(),
				'name' => $relation->get_name(),
				'type' => $relation->get_type(),
				'desc' => $relation->is_readable() ? $relation->get_attribute( OBJ_DESC ) : '',
				'is_readable' => $relation->is_readable(),
				'is_writeable' => $relation->is_writeable()
			);

		// build type array
		$relations[] = array(
			'type' => $relation_type,
			'protected' => $object->is_relation_protected( $relation_type ),
			'objects' => $objs
		);
	}

	// smarty
	$smarty->assign(array(
		'relations' => $relations,
		'is_writeable' => $object->is_writeable()
	));

	// JSON
	$ajaxBridge->append(array(
		'html_code' => $smarty->fetch('relations.tpl')
	));
}

?>